<template>
  <view class="instrument">
    <scroll-view @scrolltolower="scrolltolower" refresher-background="rgba(0, 0, 0, 0)" class="scrollView" refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="100" @refresherrefresh="onRefresh" @refresherrestore="onRestore" :style="{
        height:
          'calc(' +
          screenHeight +
          'px' +
          ' ' +
          '-' +
          ' ' +
          excessHeight +
          'rpx)',
      }" scroll-y="true">
      <view class="box" v-for="(item, index) in list" :key="index" @click.stop="toDetail(item)">
        <view class="demo-avatar">
          <view class="pic">
            <image class="point-tag" @click.stop="toUserDetail(item)" :src="item.avatar"></image>
          </view>
          <view class="name" @click.stop="toUserDetail(item)">
            {{ item.nickname }}</view>
          <view class="time"> {{ item.createtime }}</view>
        </view>

        <view class="demo-topic">
          {{ item.title }}
        </view>
        <view class="demo-title">
          {{ item.main_content }}
        </view>

        <view class="img-st" v-if="item.images || item.images.length != 0">
          <image class="img-tag" :src="item.images[index]" v-for="(i, index) in 3" :key="index"></image>
        </view>
        <view class="demo-tag-address">
          <view class="demo-tag demo-address">
            <view class="pic">
              <image class="img-tag" src="/static/images/indexpage/address-icon.png"></image>
            </view>
            <view class="price"> {{ item.address_text }}</view>
          </view>
          <view class="demo-tag" style="padding-left: 10rpx;">
            <view class="pic">
              <image style="margin-top: 1rpx;" class="img-tag" src="/static/images/indexpage/topic-icon.png"></image>
            </view>
            <view class="price"> {{ item.label }}</view>
          </view>
        </view>
      </view>
      <view class="nodata-st" v-if="form.page == form.last_page">
        ------没有更多数据了------</view>
      <view class="nodata-st" v-if="list.length == 0">
        ------暂无数据------</view>
      <view style="height: 24rpx"></view>
    </scroll-view>
  </view>
</template>

<script>
import api from "@/api/index.js";
export default {
  name: "x-instrument",
  props: {
    screenHeight: {
      type: Number,
      default:
        uni.getStorageSync("screenHeight") -
        uni.getStorageSync("safeAreaBottom"),
    },
    excessHeight: {
      type: Number,
      default: 0,
    },
    query: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      // screenHeight: uni.getStorageSync('screenHeight') - uni.getStorageSync('safeAreaBottom'),
      // safeareabottom: uni.getStorageSync('safeAreaBottom'),
      triggered: true,
      form: {
        page: 1,
        page_size: 10,
        last_page: 0,
      },
      list: [],
    };
  },
  mounted() {
    // this.init()
    console.log(this.query);
    this._freshing = false;
    setTimeout(() => {
      this.triggered = true;
    }, 1000);
  },
  methods: {
    init() {
      this.form.page = 1;
      this.form.last_page = 0;
      this.getpage();
    },
    getpage() {
      api.base.getTrends({ ...this.form, ...this.query }).then((res) => {
        if (res.code == 1) {
          this.list =
            this.form.page === 1
              ? res.data.data
              : this.list.concat(res.data.data);
          this.form.last_page = res.data.last_page;
        } else {
          uni.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      });
    },
    onRefresh() {
      if (this._freshing) return;
      this._freshing = true;
      // this.init()
      this.$emit("initPage");
      setTimeout(() => {
        this.triggered = false;
        this._freshing = false;
      }, 1000);
    },
    onRestore() {
      this.triggered = "restore"; // 需要重置
      console.log("onRestore");
    },
    scrolltolower() {
      if (this.form.page < this.form.last_page) {
        this.form.page += 1;
        this.getpage();
      }
    },
    toDetail(item) {
      //求货
      if (item.is_adv == 1) {
        uni.navigateTo({
          url: `/pages/componets/instrument/advertisement/index?id=` + item.id,
        });
      } else {
        //3=求货,4=代加工
        if (this.query.types == 3) {
          uni.navigateTo({
            url:
              `/pages/componets/requestGoods/requestGoodsDetail/index?types=` +
              this.query.types +
              "&id=" +
              item.id,
          });
        } else {
          uni.navigateTo({
            url:
              `/pages/componets/requestGoods/contractProcessDetail/index?types=` +
              this.query.types +
              "&id=" +
              item.id,
          });
        }
      }
    },
    toUserDetail(item) {
      if (uni.getStorageSync("userinfo").user_id == item.user_id) {
        uni.switchTab({
          url: "/pages/my/my",
        });
      } else {
        uni.navigateTo({
          url: `/pages/otherPeople/index?user_id=` + item.user_id,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.instrument {
  .box {
    margin: 0rpx 32rpx 32rpx 32rpx;
    width: calc(100% - 64rpx);
    // height: 495rpx;
    background: #ffffff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    padding-bottom: 24rpx;

    .demo-avatar {
      height: 86rpx;
      line-height: 66rpx;
      padding-top: 24rpx;
      padding-left: 16rpx;
      margin-bottom: 16rpx;
      display: flex;
      position: relative;

      .pic {
        display: flex;
        justify-content: center;
        align-items: center;
        float: left;
        width: 66rpx;
        height: 66rpx;

        .point-tag {
          width: 66rpx;
          height: 66rpx;
          border-radius: 7694rpx 7694rpx 7694rpx 7694rpx;
        }
      }

      .name {
        height: 66rpx;
        line-height: 66rpx;
        padding-left: 16rpx;
        font-size: 28rpx;
        font-family: PingFang SC-Bold, PingFang SC;
        font-weight: bold;
        color: #333333;
      }

      .time {
        position: absolute;
        top: 24rpx;
        right: 18rpx;
        font-size: 20rpx;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 500;
        color: rgba(0, 0, 0, 0.24);
      }
    }

    .demo-title {
      margin: 12rpx 16rpx 0rpx 16rpx;
      width: calc(100% - 32rpx);
      font-size: 24rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 500;
      color: #666666;
      // height: 72rpx;
      line-height: 36rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }

    .demo-topic {
      margin: 15rpx 16rpx 0rpx 16rpx;
      width: calc(100% - 32rpx);
      font-size: 28rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: bold;
      color: #333333;
      // height: 36rpx;
      line-height: 36rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }

    .img-st {
      margin: 0rpx 16rpx 4rpx 16rpx;
      width: calc(100% - 32rpx);
      height: 210rpx;
      display: flex;
      .img-tag {
        border-radius: 12rpx 12rpx 12rpx 12rpx;
        flex: 1;
        // width: 210rpx;
        height: 210rpx;
        // width: 100%;
        // height: 360rpx;
        margin: 6rpx;
      }
    }

    .bottom-box {
      margin: 35rpx 16rpx 16rpx 16rpx;
      width: calc(100% - 32rpx);
      height: 40rpx;
      line-height: 40rpx;
      display: flex;

      .img {
        width: 40rpx;
        margin-right: 10rpx;

        .img-tag {
          width: 40rpx;
          height: 40rpx;
        }
      }

      .value {
        font-size: 24rpx;
        font-family: HarmonyOS Sans-Regular, HarmonyOS Sans;
        font-weight: 400;
        color: #333333;
        padding-right: 30rpx;
      }
    }

    .demo-tag-address {
      margin: 16rpx 16rpx 0rpx 16rpx;
      width: calc(100% - 32rpx);
      height: 47rpx;
      line-height: 47rpx;
      display: flex;
      justify-content: space-between;

      .demo-tag {
        padding-left: 10rpx;
        padding-right: 16rpx;
        display: flex;
        // width: 168rpx;
        height: 47rpx;
        line-height: 50rpx;
        background: rgba(109, 12, 247, 0.1);
        border-radius: 44rpx 44rpx 44rpx 44rpx;
        justify-content: center;
        align-items: center;

        .pic {
          width: 33rpx;
          height: 38rpx;
          // border-radius: 100rpx;
          // background: #6d0cf7;
          font-size: 24rpx;
          color: #ffffff;
          display: flex;
          justify-content: center;
          align-content: center;

          .img-tag {
            width: 33rpx;
            height: 33rpx;
          }
        }

        .price {
          padding-left: 14rpx;
          font-size: 24rpx;
          color: #6d0cf7;
          height: 47rpx;
          line-height: 47rpx;
          // text-align: center;
          // width: 200rpx;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          -o-text-overflow: ellipsis;
        }
      }

      .demo-address {
        padding-left: 10rpx;
        padding-right: 16rpx;
        // width: 300rpx;
        background: #f3f3f3;

        .price {
          color: #333333;
          // width: 300rpx;
          max-width: 250rpx;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          -o-text-overflow: ellipsis;
        }

        .pic {
          height: 47rpx;
          line-height: 47rpx;
          width: 24rpx;
          height: 24rpx;
          // border-radius: 100rpx;
          // background: #6d0cf7;
          font-size: 24rpx;
          color: #ffffff;
          display: flex;
          justify-content: center;
          align-content: center;

          .img-tag {
            width: 24rpx;
            height: 24rpx;
          }
        }
      }
    }
  }
}
</style>